package com.summer_administration.controller;

import com.summer_administration.mapper.GroupeMapper;
import com.summer_administration.pojo.*;
import com.summer_administration.service.ExpensesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/expenses")
@Validated
public class expensesController {
    @Autowired
    private ExpensesService expenseService;

    @Autowired
    private GroupeMapper groupeMapper;

    @PostMapping
    public Result add(@RequestBody @Validated Expenses expenses) {
        // 新增一个根剧小组名字查询小组id
        String groupeName = expenses.getGroupName();
        String groupId = groupeMapper.findGroupIdByGroupName(groupeName);
        expenses.setGroupId(groupId);
        expenseService.add(expenses);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Expenses>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize){
        PageBean<Expenses> pb = expenseService.list(pageNum, pageSize);
        return Result.success(pb);
    }

    @PutMapping
    public Result update(@RequestBody @Validated Expenses expenses) {
        expenseService.update(expenses);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        expenseService.delete(id);
        return Result.success();
    }


    @GetMapping("/group")
    public Result<List<GroupExpenseId>> groupExpenses() {
        List<GroupExpenseId> list = expenseService.getGroupExpenses();
        return Result.success(list);
    }

    //根据⽀付⽅式统计⽀出⾦额
    @GetMapping("/byMethod")
    public Result<List<GroupExpenseDTO>> expensesByMethodd() {
        List<GroupExpenseDTO> list = expenseService.getExpensesByMethod();
        return Result.success(list);
    }

    // 根据是否报销统计⽀出⾦额
    @GetMapping("/byState")
    public Result<List<StateExpenseDTO>> expensesByState() {
        List<StateExpenseDTO> list = expenseService.getExpensesByState();
        return Result.success(list);
    }
}
